import xml.dom.minidom
from xml.dom.minidom import Node
	

class Trip:
	def __init__(self, docName):
		self.people = []
		self.region = ""
		self.period = ""
		self.albumTitle = ""
		self.albumImages = []
		journal = xml.dom.minidom.parse(docName)	
		self.handlePeople(journal.getElementsByTagName("People"))
		self.handleRegion(journal.getElementsByTagName("Region"))
		self.handlePeriod(journal.getElementsByTagName("Period"))
		self.handleAlbum(journal.getElementsByTagName("Album"))
	
	def handlePeople(self, people):
		for person in people:
			for i in person.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.people.append(i.data)
					
	def handleRegion(self, regions):
		for region in regions:
			for i in region.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.region =i.data

	def handlePeriod(self, period):
		for periods in period:
			for i in periods.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.period = i.data

	def handleAlbum(self, albums):
		for album in albums:
			self.handleAlbumTitle(album.getElementsByTagName("Title"))
			self.handleImages(album.getElementsByTagName("Image"))
		
	def handleAlbumTitle(self, title):
		for titles in title:
			for i in titles.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.albumTitle = i.data
		
	def handleImages(self, images):
		for image in images:
			newImage = Image(image)
			self.albumImages.append(newImage)
			
	def printTrip(self):
		print "Trip"
		print "\tPeople: "
		for person in self.people:
			print "\t\t"+person
		print "\tRegion: "+self.region
		print "\tPeriod: " +self.period
		print "\tAlbum: " + self.albumTitle
		for image in self.albumImages:
			image.printImage()
			
	def printTripXML(self, fileName):
		f = open(fileName, 'w')
		f.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n")
		f.write("<!DOCTYPE traveljournal SYSTEM \"traveljournal.dtd\">\n")
		f.write("<traveljournal name=\"House1\">\n")
		for person in self.people:
			f.write("\t<People>" + person + "</People>\n")
		f.write("\t<Region>" + self.region + "</Region>\n")
		f.write("\t<Period>" + self.period + "</Period>\n")
		f.write("\t<Album>\n")
		f.write("\t\t<Title>"+ self.albumTitle +"</Title>\n")
		for image in self.albumImages:
			image.printImageXML(f)
		f.write("\t</Album>\n")
		f.write("</traveljournal>")
	
class Image:
	def __init__(self, image):
		self.photo = ""
		self.persons = []
		self.location = ""
		self.time = ""
		self.date = []
		self.description = ""
		self.handleImage(image)
		
	def handleImage(self, image):
		self.handlePhoto(image.getElementsByTagName("Photo"))
		self.handlePersons(image.getElementsByTagName("Persons"))
		self.handleLocation(image.getElementsByTagName("Location"))
		self.date.append(Date(image.getElementsByTagName("Date")))
		self.handleTime(image.getElementsByTagName("Time"))
		self.handleDescription(image.getElementsByTagName("Description"))
	
	def handlePhoto(self, photo):
		for photos in photo:
			for i in photos.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.photo = i.data

	def handlePersons(self, persons):
		for person in persons:
			for i in person.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.persons.append(i.data)

	def handleLocation(self, location):
		for locations in location:
			for i in locations.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.location =i.data
					
	def handleTime(self, time):
		for times in time:
			for i in times.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.time = i.data

	def handleDescription(self, descript):
		for descripts in descript:
			for i in descripts.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.description = i.data	
					
	def printImage(image):
		print "\t\tPhoto: :"
		print "\t\t\tFile: " + image.photo
		print "\t\t\tPeople In Photo: "
		for person in image.persons:
			print "\t\t\t\t"+person
		print "\t\t\tLocation: " + image.location
		print "\t\t\tDate: " + str(image.date[0].month) + "/" + str(image.date[0].day) + "/" + str(image.date[0].year)
		print "\t\t\tTime: " + image.time
		print "\t\t\tDescription: " + image.description
		
	def printImageXML(self, f):
		f.write("\t\t<Image>\n")
		f.write("\t\t\t<Photo>" + self.photo + "</Photo>\n")
		for person in self.persons:
			f.write("\t\t\t<Persons>" + person + "</Persons>\n")
		f.write("\t\t\t<Location>" + self.location + "</Location>\n")
		f.write("\t\t<Date>\n")
		f.write("\t\t\t<Month>"+ str(self.date[0].month) + "</Month>\n")
		f.write("\t\t\t<Day>"+ str(self.date[0].day) + "</Day>\n")
		f.write("\t\t\t<Year>"+ str(self.date[0].year) + "</Year>\n")
		f.write("\t\t</Date>\n")
		f.write("\t\t\t<Time>" + self.time + "</Time>\n")
		f.write("\t\t\t<Description>" + self.description + "</Description>\n")
		f.write("\t\t</Image>\n")
		
class Date:
	def __init__(self, date):
		self.month = 0
		self.day = 0
		self.year = 0
		self.handleDate(date)
		
	def handleDate(self, dates):
		for date in dates:
			self.handleMonth(date.getElementsByTagName("Month"))
			self.handleDay(date.getElementsByTagName("Day"))
			self.handleYear(date.getElementsByTagName("Year"))
		
	def handleMonth(self, month):
		for months in month:
			for i in months.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.month = i.data
					
	def handleDay(self, day):
		for days in day:
			for i in days.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.day = i.data
					
	def handleYear(self, year):
		for years in year:
			for i in years.childNodes:
				if i.nodeType == Node.TEXT_NODE:
					self.year = i.data
		

trip = Trip("tester.xml")

trip.printTrip()

trip.printTripXML("newTrip.xml")

newTrip = Trip("newTrip.xml")

newTrip.printTrip()

print trip.people
print newTrip.people
print trip.albumImages[0].date[0].day
